import os

from def_pack.normal_solve import read_content_order22, str_transfer1, out_name2, write_file, to_json_file, \
    get_jsonfile, list_to_jsonfile


def save_cache_file(kk,basc):


    stryy=str_transfer1(kk)

    cachefilepath = f"{basc}/data/缓存文档的.txt"
    write_file(cachefilepath, stryy)
    kk = read_content_order22(cachefilepath)

    return kk

def replace_sign(list1):
    kk=list1
    lmm = 0

    ##对kk数组切分出句号的内容。
    for yi in range(len(kk)):

        if str(kk[yi]) != "":
            lmm = lmm + 1
            # print("句号换行：" + str(lmm) + ":" + str(len(kk)))

            # 只针对第一个进行筛选，并不是对全段进行检查筛选的，这个可以放心
            llo = kk[yi][0]
            if str(llo).find("‘") == -1 and str(llo).find("“") == -1 and str(llo).find("\"") == -1 and str(llo).find(
                    "'") == -1 \
                    and str(llo).find("「") == -1 and str(llo).find("「") == -1 and str(llo).find("[") == -1 and str(
                llo).find("]") == -1 \
                    and str(llo).find("【") == -1 and str(llo).find("】") == -1 and str(llo).find("『") == -1 and str(
                llo).find("』") == -1 \
                    and str(llo).find("（") == -1 and str(llo).find("）") and str(llo).find("(") == -1 and str(llo).find(
                ")"):
                kk[yi] = str(kk[yi]).replace("。", "。\n")
                kk[yi] = str(kk[yi]).replace(".", ".\n")
    return kk

def replace_no_need_word(basc,kk):
    filterfile = f"{basc}/data/切片后的不需要的首字.txt"
    fk3 = open(filterfile, "r", encoding="utf-8")
    grafk = fk3.readlines()
    fk3.close()

    # 替换后的数组,就是kk3
    aftaa = []

    ssxshuzu = []

    # 再次替换一下
    for yi in range(len(kk)):
        # print('筛选掉词头:' + str(yi) + ":" + str(len(kk)))

        # ‘商队kk3[yi][0]
        if str(kk[yi]) != "":
            llo = kk[yi][0]

        if len(str(kk[yi])) > 1:
            ssx = str(kk[yi])

            for jju in range(len(grafk)):

                # ~ #多个字，就筛选多个字
                if str(grafk[jju]) != "":
                    ctsl = len(grafk[jju])
                    ctsl2 = int(ctsl) + int(1)
                    # 不对，应该检测下关键字的数量，然后再决定截取多少个词头，作为对照的。
                    # ~ ctf=str(kk3[yi])[0:int(ctsl2)]
                    ctf = str(kk[yi])[0]
                    if str(ctf).find(str(grafk[jju][0])) != -1:
                        # ~ print(str(ctf)+":"+str(grafk[jju]))

                        # 这时候，就是找到词头了
                        kk[yi] = ""
                        ssx = str(kk[yi])
                        # 将这句话的内容改为空，就是删掉这句话。

                        # 替换掉了，就break
                        break

            ssxshuzu.append(ssx)
        # ~ print(str(ctf))

    # ~ print()
    kkkl = len(ssxshuzu)
    mmki = 0
    for y2 in ssxshuzu:
        # print(f"替换掉ha：{mmki}/{kkkl}")
        y2 = str(y2).replace("\xa0", "")
        y2 = str(y2).replace("\ufffd", "")
        # y2 = str(y2).replace("ha", "")
        y2 = str(y2).replace("\n", "")
        mmki = mmki + 1
        if len(str(y2)) > 1:
            aftaa.append(y2)
    return aftaa

def delete_much_prag_and_change_speak_way(aftaa):
    # 删除大段行之后的数组
    aftaa2 = []

    # 删除大段的行
    kkkl = len(aftaa)
    mmki = 0
    for y3 in aftaa:
        # 每段的字符重置数
        strii = 0
        mmki = mmki + 1
        # print(f"删除大段行：{mmki}/{kkkl}")
        for soo in y3:
            # 统计字符数
            if '\u4e00' <= soo <= '\u9fff':
                strii = strii + 1

        # 判断字符数，然后加入数组
        if strii < 54:
            aftaa2.append(y3)

    # 修改说话方式
    aftaa3 = []

    # 删除大段的行
    kkkl = len(aftaa2)
    mmki = 0
    for y3 in aftaa2:
        # #每段的字符重置数
        # strii=0
        mmki = mmki + 1

        y3 = str(y3).replace("已经", "")
        # “的”不能删掉，因为使用的情况比较多
        y3 = str(y3).replace("虽然", "")
        y3 = str(y3).replace("如果", "")
        y3 = str(y3).replace("不只是", "")
        y3 = str(y3).replace("只是", "")
        y3 = str(y3).replace("但是", "")
        y3 = str(y3).replace("但", "")
        y3 = str(y3).replace("然后", "")
        # y3 = str(y3).replace("也", "")
        y3 = str(y3).replace("应该", "")
        # 一些模糊的词，也要替换掉
        y3 = str(y3).replace("不止是", "")
        y3 = str(y3).replace("而是", "")
        y3 = str(y3).replace("居然", "")
        # “有”字的不能删除
        y3 = str(y3).replace("只要", "")
        y3 = str(y3).replace("果然", "")
        y3 = str(y3).replace("不管怎样", "")
        y3 = str(y3).replace("所以", "")
        y3 = str(y3).replace("那是", "")
        # “并”字不能替换
        # “并且”字不能替换
        # “而且”字不能替换
        y3 = str(y3).replace("像是", "")
        # “不是”的不能替换
        # “不过”的不能替换
        # “正要”的不能替换
        # “吧”的不能替换
        y3 = str(y3).replace("能够", "")
        y3 = str(y3).replace("尽快", "")
        y3 = str(y3).replace("究竟是", "")
        y3 = str(y3).replace("那真是", "")
        y3 = str(y3).replace("再说", "")
        y3 = str(y3).replace("究竟", "")
        y3 = str(y3).replace("而已", "")
        y3 = str(y3).replace("差不多", "")
        y3 = str(y3).replace("则是", "")
        y3 = str(y3).replace("看起来", "")
        y3 = str(y3).replace("即使是", "")
        y3 = str(y3).replace("即使", "")
        y3 = str(y3).replace("但仍然", "")
        y3 = str(y3).replace("仍然", "")
        y3 = str(y3).replace("理所当然地", "")
        y3 = str(y3).replace("理所当然的", "")
        y3 = str(y3).replace("大概是", "")
        y3 = str(y3).replace("大概", "")
        y3 = str(y3).replace("听起来", "")
        y3 = str(y3).replace("仍然", "")
        y3 = str(y3).replace("就这样", "")
        y3 = str(y3).replace("那么", "")
        y3 = str(y3).replace("不自觉地", "")
        y3 = str(y3).replace("这样的话", "")
        y3 = str(y3).replace("看来是", "")



        aftaa3.append(y3)

    # 这上面就是形成一个数组的，也可以说是结果数组。
    return aftaa3
def count_word(srt):
    strii = 0
    for soo in str(srt):
        # 统计字符数
        if '\u4e00' <= soo <= '\u9fff':
            strii = strii + 1
    return strii

def recycle_1(listap,wordstr):


    wordstrf=str(wordstr)[:54]
    depar_place2 = int(str(wordstrf).rfind("，")) + 1
    if depar_place2==0:
        word3 = str(wordstr)[:54]
        wordstr = str(wordstr)[54:]
    else:
        word3 = str(wordstr)[:int(depar_place2)]
        wordstr = str(wordstr)[int(depar_place2):]
    listap.append(word3)
    return listap,wordstr

def recycle_detect_word(listap,wordstr):
    word_num2 = count_word(wordstr)
    while int(word_num2) > 54:
        listap,wordstr=recycle_1(listap, wordstr)
        word_num2 = count_word(wordstr)
    listap.append(wordstr)
    return listap

#这是正常生成的，把大段分成小段的。
def filter_row_noval_list(aftaa):
    # aftaa=[]
    # for y2 in aftaa77:
    #     y2 = str(y2).replace("\n", "")
    #     if len(y2)>0:
    #         aftaa.append(y2)


    # 删除大段行之后的数组
    aftaa2 = []

    # 删除大段的行
    kkkl = len(aftaa)
    mmki = 0
    for y3 in aftaa:
        # 每段的字符重置数
        strii = 0
        mmki = mmki + 1
        # print(f"删除大段行：{mmki}/{kkkl}")
        word_num=count_word(str(y3))
        if int(word_num) > 54:
            aftaa2=recycle_detect_word(aftaa2, str(y3))
        else:
            aftaa2.append(y3)
    return aftaa2

        # if int(word_num) > 54:
        #     depar_place=int(str(y3).rfind("，"))+1
        #     word1=str(y3)[:int(depar_place)]
        #     word2=str(y3)[int(depar_place):]
        #     aftaa2.append(word1)
        #
        #     word_num2 = count_word(word2)
        #     while int(word_num2)>54:
        #         depar_place2 = int(str(word2).rfind("，")) + 1
        #         word3 = str(word2)[:int(depar_place2)]
        #         word4 = str(word2)[int(depar_place2):]
        #         aftaa2.append(word3)
        #         word_num2 = count_word(word4)
        #
        #
        #     aftaa2.append(word2)



        # 判断字符数，然后加入数组
        # if strii < 54:
        #     aftaa2.append(y3)

def list_filter_nume_novel(basc,list1,jsonfile,type=0):
    if not os.path.exists(jsonfile):
        kk=list1
        # 这时候，就是要对那个字符进行筛选的。
        # 编码格式
        encodezdy = 'utf-8'

        #叙述类句号分开
        kk=replace_sign(kk)

        #保存到缓存文档里面,再重新读取数组出来。
        kk=save_cache_file(kk, basc)

        list_to_jsonfile(kk, jsonfile)
    else:
        print("处理后的json文件已存在，读取原文件")
        filedict = get_jsonfile(jsonfile)
        kk = []
        for ii in filedict.keys():
            uii = filedict.get(ii)
            kk.append(uii)

    if type==1:
        aftaa3=filter_row_noval_list(kk)
        return aftaa3
    else:
        #把不需要的首字替换掉，或者说是个筛选。
        #如果是全文再生成的话，就不需要这个了。
        aftaa=replace_no_need_word(basc, kk)

        #
        ctf = ""

        ssx = ""
        #这个不筛选的话，也是不需要的，或者，用的是另一种的替换方式
        aftaa3=delete_much_prag_and_change_speak_way(aftaa)
        return aftaa3





def read_simple_noval(basc,novalfile,type=0):
    if type==1:
        outfile = out_name2(basc, novalfile, "再生成")
    else:
        outfile = out_name2(basc, novalfile, "省略")
    if not os.path.exists(outfile):
        outjsonfile = out_name2(basc, novalfile, 'j', out="jsondata",back='json')
        if not os.path.exists(outjsonfile):
            outjsonfile=to_json_file(basc,novalfile)
        else:
            print('已存在文件，读取原json文件')
            # outfile=outjsonfile
        filedict = get_jsonfile(outjsonfile)
        listaa2 = []
        for ii in filedict.keys():
            uii = filedict.get(ii)
            listaa2.append(uii)
        # listaa2 = read_content_order22(novalfile)
        jsonfile=out_name2(basc, novalfile, '处理后j', out="jsondata",back='json')
        list_have_filter=list_filter_nume_novel(basc, listaa2,jsonfile,type)

        stryy=str_transfer1(list_have_filter)

        write_file(outfile, stryy)
    else:
        print("文件已存在，不再处理")
    return outfile

if __name__ == '__main__':
    pass
